package com.dwjm.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dwjm.entity.SysUsers;
import com.dwjm.service.SysUsersService;
import com.dwjm.util.Md5Encrypt;
import com.jcraft.jsch.Logger;

/**
 * @Description 用户登录Controller
 * @author luans
 * @date 2016年12月19日
 *
 */
@Controller
@RequestMapping("userctr")
public class UserController {
	

	@Resource
	private SysUsersService sysUsersService;
	
	
	
	/**
	 * 
	 * @Description 后台：跳往管理员列表页面
	 * @param adduserid:管理员id
	 * @return ModelAndView
	 * @author luans
	 * @date 2017年10月21日
	 */
	@RequestMapping("tosyslist")
	public ModelAndView sysUserList(SysUsers sysuser) {
		ModelAndView mv = new ModelAndView();
		SysUsers sys=sysUsersService.selectByPrimaryKey(sysuser.getAdduserid());
	    List<SysUsers> list=sysUsersService.sysUserList(sysuser);
	    
	    
	    mv.addObject("list", list);
	    mv.addObject("sysuserlogin", sys);
	    mv.addObject("sysuser", sysuser);
		mv.setViewName("manager/user/syslist");
		return mv;
	}
	/**
	 * 
	 * @Description 后台：去管理员操作页面
	 * @param id:管理员id
	 * @return ModelAndView
	 * @author luans
	 * @date 2017年10月21日
	 */
	@RequestMapping("tomanagesysuser")
	public ModelAndView tosysUserManage(SysUsers sysuser) {
   System.out.println("sysuser.getAdduserid():"+sysuser.getAdduserid());		
		ModelAndView mv = new ModelAndView();
		//验证登录管理员身份
		/*if(sysuser.getAdduserid()<=0){
			//确认管理员身份error,不是超级管理员身份，无权限添加管理员
			//跳转至登录页面，重新登录
			mv.setViewName("manager/system/login");
			return mv;
		}		*/
	/*	SysUsers sys=sysUsersService.selectByPrimaryKey(sysuser.getAdduserid());
		if(sys.getSysuserId()<=0 || sys.getSysuserState()!=0 || sys.getUserType()!=1){
			//确认管理员身份error,不是超级管理员身份，无权限添加管理员
			//跳转至登录页面，重新登录
			mv.setViewName("manager/system/login");
			return mv;
		}*/
		
		if(sysuser.getMarkManage()==1){
			//生成登录账号
			Integer count=sysUsersService.countSumNum()+1;
			sysuser.setSysuserName("admin"+count);
			mv.addObject("sysuser", sysuser);
		}else if(sysuser.getMarkManage()==2){
			SysUsers sysusernew=sysUsersService.selectByPrimaryKey(sysuser.getSysuserId());
			sysusernew.setMarkManage(2);
			sysusernew.setAdduserid(sysuser.getAdduserid());
			mv.addObject("sysuser", sysusernew);
		}
		
		mv.setViewName("manager/user/sysmanage");
		return mv;
	}
	
	/**
	 * 
	 * @Description 后台：添加管理员操作
	 * userctrl/ managesysuser.do
	 * @return ModelAndView
	 * @author luans
	 * @date 2017年10月21日
	 */
	@RequestMapping("managesysuser")
	public ModelAndView sysUserManage(SysUsers sysuser) {
		ModelAndView mv = new ModelAndView();
		/*//验证登录管理员身份
		if(sysuser.getAdduserid()==null || sysuser.getAdduserid()<=0){
			//确认管理员身份error,不是超级管理员身份，无权限添加管理员
			//跳转至登录页面，重新登录
			mv.setViewName("manager/system/login");
			return mv;
		}		
		SysUsers sys=sysUsersService.selectByPrimaryKey(sysuser.getAdduserid());
		if(sys.getSysuserId()<=0 || sys.getSysuserState()!=0 || sys.getUserType()!=1){
			//确认管理员身份error,不是超级管理员身份，无权限添加管理员
			//跳转至登录页面，重新登录
			mv.setViewName("manager/system/login");
			return mv;
		}	*/
		
		if(sysuser.getMarkManage()==1){
			// 生成密码
			String pwdcode = RandomStringUtils.randomAlphanumeric(8);// 生成8位随机字符串
			String pwd = Md5Encrypt.md5(sysuser.getSysuserPwd() + pwdcode) + pwdcode;
			sysuser.setSysuserPwd(pwd);
			sysUsersService.insertSelective(sysuser);
			
		}else if(sysuser.getMarkManage()==2){
			if(!"nochange".equals(sysuser.getSysuserPwd())){
				System.out.println("sysuser.getSysuserPwd():"+sysuser.getSysuserPwd());
				// 生成新密码
				String pwdcode = RandomStringUtils.randomAlphanumeric(8);// 生成8位随机字符串
				String pwd = Md5Encrypt.md5(sysuser.getSysuserPwd() + pwdcode) + pwdcode;
				sysuser.setSysuserPwd(pwd);
			}
			sysUsersService.updateByPrimaryKey(sysuser);
		}else if(sysuser.getMarkManage()==4){
			sysuser.setSysuserState(1);
			sysUsersService.updateByPrimaryKey(sysuser);
			
		}
		
		return sysUserList(sysuser);
	}

}
